package com.ruantu.clothing.controller.report;

import com.ruantu.clothing.entity.JsonResponseMessage;
import com.ruantu.clothing.entity.PageBean;
import com.ruantu.clothing.entity.report.TailorReport;
import com.ruantu.clothing.service.report.TailorReportService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;

/**
 * Created by Administrator on 2018/8/8 0008.
 */
@Controller
@RequestMapping("/tailorreport")
public class TailorReportController {

    @Autowired
    private TailorReportService tailorReportService;

    /**
     * 查询裁剪报表
     *
     * @param tailorReport
     * @param pageBean
     * @param startTime
     * @param endTime
     * @param fenlei
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public JsonResponseMessage saveChuKu(TailorReport tailorReport, String sort, String order, PageBean pageBean, String startTime, String endTime, Integer fenlei) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        jsonResponseMessage.setRows(tailorReportService.findListReport(tailorReport, sort, order, pageBean, startTime, endTime, fenlei));
        jsonResponseMessage.setTotal(pageBean.getTotal());
        jsonResponseMessage.setFooter(tailorReportService.findTailorSum(tailorReport, sort, order, startTime, endTime));
        return jsonResponseMessage;
    }

    /**
     * 导出excel模板
     *
     * @param out
     * @param response
     */
    @RequestMapping("/downloadtailorreport")
    @ResponseBody
    public void daoChuMobanOrderUser(ServletOutputStream out, HttpServletResponse response, TailorReport tailorReport, String startTime, String endTime, Integer fenlei) {
        try {
            XSSFWorkbook wb = tailorReportService.downloadExcel(tailorReport, startTime, endTime, fenlei);
            HttpHeaders headers = new HttpHeaders();
            ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
            String fileName = URLEncoder.encode("裁剪报表导出数据.xls", "utf-8");
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            wb.write(out);
            outByteStream.flush();
            outByteStream.close();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

}
